/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package com.cardinalsolutions.circleprogressindicator;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Component.DrawTask;
import ohos.agp.components.Component.EstimateSizeListener;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.SweepShader;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

/**
 * CircleProgressIndicator
 *
 * @since : 2021.5.24
 */
public class CircleProgressIndicator extends Component implements DrawTask, EstimateSizeListener {
    private int startColor = -16711699;
    private int endColor = -1244929;
    private int defaultColor = 452984831;
    private int percentEndColor;
    private int strokeWidth = 100;
    private float percent = 10f;
    private Paint paint;
    private Paint startPaint;
    private Paint endPaint;
    private final RectFloat rectF = new RectFloat();
    private Color[] customColors;
    private Color[] fullColors;
    private Color[] emptyColors;
    private float[] customPositions;
    private float[] extremePositions;
    private int deltaR;
    private int deltaB;
    private int deltaG;
    private int startR;
    private int startB;
    private int startG;

    /**
     * 构造器
     *
     * @param var1 上下文
     */
    public CircleProgressIndicator(Context var1) {
        super(var1);
    }

    /**
     * 构造器
     *
     * @param var1 上下文
     * @param var2 属性
     */
    public CircleProgressIndicator(Context var1, AttrSet var2) {
        super(var1, var2);
        this.init(var1, var2);
    }

    /**
     * 构造器
     *
     * @param var1 上下文
     * @param var2 属性
     * @param var3 字符串
     */
    public CircleProgressIndicator(Context var1, AttrSet var2, String var3) {
        super(var1, var2, var3);
        this.init(var1, var2);
    }

    /**
     * 构造器
     *
     * @param var1 上下文
     * @param var2 属性
     * @param var3 int
     */
    public CircleProgressIndicator(Context var1, AttrSet var2, int var3) {
        super(var1, var2, var3);
        this.init(var1, var2);
    }

    private void init(Context var1, AttrSet var2) {
        float var3 = 0.0F;
        int var4 = (int) ((double) (this.vpToPx(this.getContext(), 18.0F)) + 0.5F);
        if (var1 != null && var2 != null) {
            AttrUtil var5 = new AttrUtil(var2);
            this.percent = var5.getFloatValue("mpc_percent", var3);
            this.strokeWidth = var5.getDimensionValue("mpc_stroke_width", var4);
            this.startColor = var5.getColorValue("mpc_start_color", this.startColor);
            this.endColor = var5.getColorValue("mpc_end_color", this.endColor);
            this.defaultColor = var5.getColorValue("mpc_default_color", this.defaultColor);
        } else {
            this.percent = var3;
            this.strokeWidth = var4;
        }

        this.paint = new Paint();
        this.paint.setAntiAlias(true);
        this.paint.setStrokeWidth((float) this.strokeWidth);
        this.paint.setStyle(Paint.Style.STROKE_STYLE);
        this.paint.setStrokeJoin(Paint.Join.ROUND_JOIN);
        this.paint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        this.startPaint = new Paint();
        this.startPaint.setColor(new Color(this.startColor));
        this.startPaint.setAntiAlias(true);
        this.endPaint = new Paint();
        this.endPaint.setAntiAlias(true);
        this.refreshDelta();
        this.customColors = new Color[]{new Color(this.startColor), new Color(this.percentEndColor),
                new Color(this.defaultColor), new Color(this.defaultColor)};
        this.fullColors = new Color[]{new Color(this.startColor), new Color(this.endColor)};
        this.emptyColors = new Color[]{new Color(this.defaultColor), new Color(this.defaultColor)};
        this.customPositions = new float[4];
        this.customPositions[0] = 0.0F;
        this.customPositions[3] = 1.0F;
        this.extremePositions = new float[]{0.0F, 1.0F};
        this.addDrawTask(this);
        this.setEstimateSizeListener(this);
    }

    private void refreshDelta() {
        int var1 = (this.endColor & 16711680) >> 16;
        this.startR = (this.startColor & 16711680) >> 16;
        this.deltaR = var1 - this.startR;
        int var2 = (this.endColor & '\uff00') >> 8;
        this.startG = (this.startColor & '\uff00') >> 8;
        this.deltaG = var2 - this.startG;
        int var3 = this.endColor & 255;
        this.startB = this.startColor & 255;
        this.deltaB = var3 - this.startB;
    }

    /**
     * 获取进度
     *
     * @return 进度值
     */
    public float getPercent() {
        return this.percent;
    }

    /**
     * 设置进度值
     *
     * @param var1 进度值
     */
    public void setPercent(float var1) {
        if (this.percent != var1) {
            this.percent = var1;
            this.invalidate();
        }
    }

    /**
     * 设置起始颜色
     *
     * @param var1 颜色值
     */
    public void setStartColor(int var1) {
        if (this.startColor != var1) {
            this.startColor = var1;
            this.refreshDelta();
            this.customColors[0] = new Color(var1);
            this.startPaint.setColor(new Color(var1));
            this.fullColors[0] = new Color(var1);
            this.invalidate();
        }
    }

    /**
     * 设计结束时的颜色
     *
     * @param var1 颜色值
     */
    public void setEndColor(int var1) {
        if (this.endColor != var1) {
            this.endColor = var1;
            this.refreshDelta();
            this.fullColors[1] = new Color(var1);
            this.invalidate();
        }
    }

    /**
     * 设置背景颜色
     *
     * @param var1 背景颜色值
     */
    public void setDefaultColor(int var1) {
        if (this.defaultColor != var1) {
            this.defaultColor = var1;
            this.customColors[2] = new Color(var1);
            this.customColors[3] = new Color(var1);
            this.emptyColors[0] = new Color(var1);
            this.emptyColors[1] = new Color(var1);
            this.invalidate();
        }
    }

    /**
     * 获取背景颜色
     *
     * @return 默认颜色Int值
     */
    public int getDefaultColor() {
        return this.defaultColor;
    }

    /**
     * 设置进度条的宽度
     *
     * @param var1 宽度值
     */
    public void setStrokeWidth(int var1) {
        if (this.strokeWidth != var1) {
            this.strokeWidth = var1;
            this.paint.setStrokeWidth((float) var1);
            this.invalidate();
        }
    }

    /**
     * 获取进度条的宽度
     *
     * @return 默认宽度值
     */
    public int getStrokeWidth() {
        return this.strokeWidth;
    }

    private void calculatePercentEndColor(float var1) {
        this.percentEndColor = ((int) ((double) this.deltaR * var1 +
                (double) this.startR) << 16) + ((int) ((double) this.deltaG * var1 + (double) this.startG) << 8) +
                (int) ((double) this.deltaB * var1 + (double) this.startB) + -16777216;
    }

    @Override
    public boolean onEstimateSize(int var1, int var2) {
        this.rectF.left = (float) (EstimateSpec.getSize(var1) / 2 - this.strokeWidth / 2);
        this.rectF.top = 0.0F;
        this.rectF.right = (float) (EstimateSpec.getSize(var1) / 2 + this.strokeWidth / 2);
        this.rectF.bottom = (float) this.strokeWidth;
        return false;
    }

    @Override
    public void onDraw(Component var1, Canvas var2) {
        int var3 = var2.save();
        int var4 = this.getWidth();
        int var6 = var4 / 2;
        int var8 = var4 / 2 - this.strokeWidth / 2;
        int var5 = this.getHeight();
        int var7 = var5 / 2;
        float var9 = this.percent;
        var2.save();
        var2.rotate(-90.0F, (float) var6, (float) var7);
        Color[] var10;
        float[] var11;
        if (var9 < 1.0F && var9 > 0.0F) {
            this.calculatePercentEndColor(var9);
            this.customColors[1] = new Color(this.percentEndColor);
            var10 = this.customColors;
            this.customPositions[1] = var9;
            this.customPositions[2] = var9;
            var11 = this.customPositions;
        } else if (("1.0").equals(String.valueOf(var9))) {
            this.percentEndColor = this.endColor;
            var10 = this.fullColors;
            var11 = this.extremePositions;
        } else {
            var10 = this.emptyColors;
            var11 = this.extremePositions;
        }

        SweepShader var12 = new SweepShader((float) var6, (float) var7, var10, var11);
        this.paint.setShader(var12, Paint.ShaderType.SWEEP_SHADER);
        var2.drawCircle((float) var6, (float) var7, (float) var8, this.paint);
        var2.restore();
        var2.restoreToCount(var3);
    }

    /**
     * vp转Px
     *
     * @param var1 上下文Context
     * @param var2 vp值
     * @return px值
     */
    public float vpToPx(Context var1, float var2) {
        return (float) (((double) (var1.getResourceManager().getDeviceCapability().screenDensity) / 160) * (double) var2);
    }
}
